package com.lanou.musicbox.fragment.onlinemusic;

import android.annotation.TargetApi;
import android.app.ProgressDialog;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.lanou.musicbox.R;
import com.lanou.musicbox.adapter.onlinemusic.RankingListAdapter;
import com.lanou.musicbox.base.BaseFragment;
import com.lanou.musicbox.bean.Rankings;
import com.lanou.musicbox.utils.Contant;
import com.lanou.musicbox.utils.NetWorkUtils;
import com.lanou.musicbox.utils.StringRequestUtf;
import com.lanou.musicbox.utils.VolleySingleton;

import java.util.List;

/**
 * Created by 杨海宁 on 15/10/21.
 * 网络音乐_排行榜
 */
public class RankingListFragment extends BaseFragment implements AdapterView.OnItemClickListener {
    private ListView listView;
    private StringRequestUtf stringRequestUtf;
    private List<Rankings.ContentEntity> rankingContents;

    private VolleySingleton volleySingleton;
    private RankingListAdapter adapter;
    private Rankings ranking;
    private FragmentManager manager;
    private String TAG = "lanou33";
    private boolean isConnect;
    private ProgressDialog dialog;
    private RankingListFragment fragment;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_ranking, null);
        listView = (ListView) v.findViewById(R.id.ranking_listview);
        listView.setOnItemClickListener(this);
        Log.i(TAG, "onCreateView ");
        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        volleySingleton = VolleySingleton.getInstance(getActivity());
        isConnect = NetWorkUtils.checkNet(getActivity());
        dialog=new ProgressDialog(getActivity());
        initDatas();
    }

    //解析数据
    private void initDatas() {
        String url_ranking = Contant.Url.URL_RankingList;
            stringRequestUtf = new StringRequestUtf(url_ranking, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Gson gson = new Gson();
                    Log.i("lanou33", "onResponse " + response);
                    try {  ranking = gson.fromJson(response, Rankings.class);
                    if (ranking.getContent() != null) {
                        rankingContents = ranking.getContent();
                        adapter = new RankingListAdapter(rankingContents, getActivity());
                        listView.setAdapter(adapter);
                        if (dialog.isShowing()){
                            dialog.dismiss();
                        }
                    }
                    } catch (Exception e) {
                        e.printStackTrace();
                        Toast.makeText(getActivity(),"接口出错",Toast.LENGTH_SHORT).show();
                    }

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {


                    if (isConnect){
                        Toast.makeText(getActivity(),"接口出错",Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(getActivity(), R.string.connect_error, Toast.LENGTH_SHORT).show();
                    }


                }
            });

            volleySingleton.addQueue(stringRequestUtf);
        if (isConnect){
            dialog.setMessage("数据加载中.....");
            dialog.show();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
//        if (!getActivity().isDestroyed()){
//
//                getFragmentManager()
//                        .beginTransaction()
//                        .remove(fragment);
//                        .commit();
//
//
//        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.i("lanou00", "onItemClick ");
        try {

            int type = ranking.getContent().get(position).getType();
            Log.i("lanou88889", "onItemClick_type " + type);
            manager = getActivity().getSupportFragmentManager();
            EnterTheRankingMusicFragment fragment = new EnterTheRankingMusicFragment();
            Bundle bundle = new Bundle();
            bundle.putString("type_id", type + "");
            fragment.setArguments(bundle);
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.addToBackStack(null);
            transaction.replace(R.id.fill_main, fragment);
            transaction.commit();
        } catch (Exception e) {
            e.printStackTrace();
           Toast.makeText(getActivity(),"接口出错",Toast.LENGTH_SHORT).show();
        }


    }
}
